<?php
/**
 * Created by Sublime.
 * User: chindor
 * Date: 2017/5/17
 * Time: 19:07
 */

namespace app\api\modules\v1\market\controllers;

//站内信
use app\api\components\ApiController;
use app\api\modules\v1\market\models\Message;
use Yii;
use app\api\modules\v1\market\models\BlackBoard;
use app\api\modules\v1\market\models\Operate;
use app\api\modules\v1\market\models\AppUser;
use app\models\base\ActivityLog;
use app\models\base\Project;
use app\models\base\Activity;
use yii\db\Query;
use app\api\modules\v1\market\models\Letter_z;
use app\models\base\Letter;

class LetterController extends ApiController
{
	/**
	 * [actionCount 站内信未读数量]
	 * @return [type] [数量]
	 */
	public function actionCount()
	{
		$get = Yii::$app->request->get();
		$count = (new Query())->select(['count(*) as count'])
							  ->from(Letter::tableName().' AS let')
							  ->where(['disabled'=>0,'is_read'=>'0'])
							  ->andWhere(['user_id'=>$get['user_id']])
							  ->all();
		// return $count->createCommand()->getSql();
		return parent::sendMessageJson(Message::SUCCESS,parent::$CODE_SUC,$count);
	}


	/**
	 * [actionLetter 站内信列表]
	 * @return [type] [description]
	 */
	public function actionLetter()
	{
		$get = Yii::$app->request->get();
		$letter = (new Query())->select(['pro.status as pstatus','pro.project_id as pid','pro.name as pname','act.name as aname','act.activity_id as aid','al.create_user_id as create_user_id','al.status as status','al.create_time as create_time','al.id as log_id','let.is_read as is_read','let.id as let_id'])->from(Letter::tableName().' AS let')
									->where(['let.disabled'=>0,'let.user_id'=>$get['user_id']])
									->innerJoin(ActivityLog::tableName().' AS al','al.id=let.log_id')
									->innerJoin(Project::tableName().' AS pro','pro.project_id=al.project_id')
									->innerJoin(Activity::tableName().' AS act','act.activity_id=al.activity_id')
									->andWhere(['al.disabled'=>0])
									->andWhere(['pro.disabled'=>0])
									->andWhere(['act.disabled'=>0]) 
									->orderBy(['let.id'=>SORT_DESC])
								  	->all();
		//把站内信格式化(创建人，时间，类型)
		if($letter){	
			$data = Letter_z::getletterinfo($letter);
			return parent::sendMessageJson(Message::SUCCESS,parent::$CODE_SUC,$data);
		}else{
			return parent::sendMessageJson(Message::SUCCESS,parent::$CODE_SUC);
		}
	}

	/**
	 * [actionOneread 单条私信已读]
	 * @return [type] [description]
	 */
	public function actionOneread()
	{
		$get = Yii::$app->request->get();
		//验证token
        $verifyToken = new AppUser();
        if($verifyToken->verifyAuth($get['user_id'],$get['token']) == false) return parent::sendMessageJson('用户认证不通过');

        $letter = Letter::findOne($get['letter_id']);
        if(!$letter){
        	return parent::sendMessageJson('没有此私信',parent::$CODE_ERR);
        }else{
        	$letter->is_read = '1';
        	if($letter->save()){
        		return parent::sendMessageJson('操作成功',parent::$CODE_SUC);
        	}else{
        		return parent::sendMessageJson('操作失败',parent::$CODE_ERR);
        	}
        }
	}

	/**
	 * [actionOneread 全部私信已读]
	 * @return [type] [description]
	 */
	public function actionRead()
	{
		$get = Yii::$app->request->get();
		//验证token
        $verifyToken = new AppUser();
        if($verifyToken->verifyAuth($get['user_id'],$get['token']) == false) return parent::sendMessageJson('用户认证不通过');

        $letter = Letter::updateAll(['is_read'=>1],'user_id='.$get['user_id']);
        if($letter){
        // var_dump($letter);
        	return parent::sendMessageJson('操作成功',parent::$CODE_SUC); 
        }else{
			return parent::sendMessageJson('操作失败',parent::$CODE_ERR);
        }
	}

	/**
	 * [actionOneread 全部私信清空]
	 * @return [type] [description]
	 */
	public function actionEmpty()
	{
		$get = Yii::$app->request->get();
		//验证token
        $verifyToken = new AppUser();
        if($verifyToken->verifyAuth($get['user_id'],$get['token']) == false) return parent::sendMessageJson('用户认证不通过');

        $letter = Letter::updateAll(['disabled'=>1],'user_id='.$get['user_id']);
        if($letter){
        // var_dump($letter);
        	return parent::sendMessageJson('操作成功',parent::$CODE_SUC); 
        }else{
			return parent::sendMessageJson('操作失败',parent::$CODE_ERR);
        }
	}
}